package com.vnp.cooking.httpclient;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.vnp.cooking.utils.PreferenceHelper;

public class HttpClientHelper {
	// khai bao duong dan api chung
	private static final String BASE_URL = "http://anuong.net";
	private static final String BASE_URL_DEV = "http://dev.anuong.net";
	
	private static final String NHABEP_URL = "/api/recipe";
	
	private static final String UPDATE_DEVICE = "/api/user/updateDevice";
	
	private static final String GET_ALL_CT = "/api/recipe/allName";
	
	// cong thuc chi tiet
	private static final String DETAIL_URL = "/api/recipe/detail";
	private static final String STEP_URL = "/api/recipe/stepByStep";
	private static final String SORT_URL = "/api/recipe/sort";
	private static final String CTDADANG_URL = "/api/recipe/createdList";
	private static final String CTDATHICH_URL = "/api/recipe/favoriteList";
	private static final String RATING_CT = "/api/recipe/rating";
	private static final String LIKE_CT = "/api/recipe/like";
	
	
	
	/** User */
	private static final String GET_INFO_USER = "/api/user/info";
	private static final String USER_ACTIVITY = "/api/user/activity";
	private static final String UPDATE_URL = "/api/user/updateUrl";
	private static final String UPDATE_INFO = "/api/user/update";
	private static final String GET_NOTIFI = "/api/user/getNotification";
	
	/** BST */
	private static final String GET_BST = "/api/recipe/collectionList";
	private static final String CREATE_NEW_BST = "/api/recipe/collectionCreate";
	private static final String UPDATE_BST = "/api/recipe/collectionUpdate";
	private static final String DETAIL_BST = "/api/recipe/collectionDetail";
	
	
	/** Tao mon an */
	private static final String CREATE_NEWCONGTHUC = "/api/recipe/create";
	private static final String UPLOAD_IMG_CT = "/api/recipe/imageRecipe";
	private static final String UPDATE_NGUYENLIEU = "/api/recipe/updateMaterial";
	private static final String DELETE_CT = "/api/recipe/destroy";
	private static final String UPDATE_DACDIEM = "/api/recipe/updateParams";
	private static final String SAVE_STEP_CONGTHUC = "/api/recipe/saveStep";
	private static final String SAVE_ALL_CONGTHUC = "/api/recipe/save";
	
	
	/** Search mon an */
	private static final String SEARCH_MONAN = "/api/recipe/search";
	
	/** get thuoc tinh */
	private static final String GET_ATTRIBUTE = "/api/recipe/GetAttribute";
	
	// khai bao context
	private Context context;
	
	// khai bao cac thanh phan thao tac client va server
	private static AsyncHttpClient client = new AsyncHttpClient();
	private AsyncHttpResponseHandler  asyncHttpResponseHandler;
	
	// khai bao file luu tam
	private PreferenceHelper preferenceHelper;
	
	// contructor
	public HttpClientHelper(AsyncHttpResponseHandler asyncHttpResponseHandler, Context context) {
		this.asyncHttpResponseHandler = asyncHttpResponseHandler;
		this.context = context;
		preferenceHelper = new PreferenceHelper(context);
	}
	
	public HttpClientHelper(AsyncHttpResponseHandler asyncHttpResponseHandler) {
		this.asyncHttpResponseHandler = asyncHttpResponseHandler;
	}
	
	public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
		client.get(getAbsoluteUrl(BASE_URL, url), params, responseHandler);
	}
	
	// phan rieng
	public static void get(String url, AsyncHttpResponseHandler responseHandler) {
		client.get(url, null, responseHandler);
	}

	public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
		client.post(getAbsoluteUrl(BASE_URL,url), params, responseHandler);
	}
	
	public static void postDev(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
		client.post(getAbsoluteUrl(BASE_URL_DEV,url), params, responseHandler);
	}
	
	// get url voi tham so truyen len
	private static String getUrlWithParams(String base, String relativeUrl, List<BasicNameValuePair> params) {
		String url = getAbsoluteUrl(base, relativeUrl);
		for (int i = 0; i < params.size(); i++) {
			BasicNameValuePair b = params.get(i);
			url+="&"+b.getName()+"="+b.getValue();
		}
		url = url.replace(" ", "%20");
		return url;
	}
	
	// get url post
	private static String getUrlPostWithParams(String base, String relativeUrl, RequestParams params) {
		String url = getAbsoluteUrl(base, relativeUrl);
		RequestParams r = params;
		url+="?"+r;
		url = url.replace(" ", "%20");
				
		return url;
	}
	
	
	// tao duong dan api 
	private static String getAbsoluteUrl(String base, String relativeUrl) {
		return base + relativeUrl + "/";
	}
	
	// trang nha bep
	public void getDataNhabep(int page) {
		RequestParams params = new RequestParams();
		params.add("page_number", String.valueOf(page));
		post(NHABEP_URL+"?", params, asyncHttpResponseHandler);
		Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, NHABEP_URL, params));
	}
	
	// trang detailcong thuc
	public void getDetailCongthuc(String recipe_id,String userId) {
		RequestParams params = new RequestParams();
		params.add("recipe_id", recipe_id);
		params.add("user_id", userId);
		post(DETAIL_URL+"?", params, asyncHttpResponseHandler);
		Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, DETAIL_URL, params));
	}
	
	// lay cac buoc lam
	public void getStepCongthuc(String recipe_id){
		RequestParams params = new RequestParams();
		params.add("recipe_id", recipe_id);
		post(STEP_URL+"?", params, asyncHttpResponseHandler);
		Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, STEP_URL, params));
	}
	
	public void sortMonAn(int page, String type) {
		RequestParams params = new RequestParams();
		params.add("page_number", String.valueOf(page));
		params.add("type", type);
		
		post(SORT_URL+"?", params, asyncHttpResponseHandler);
		Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, SORT_URL, params));
	}
	
	// lay cong thuc da dang
	public void getCongThucDaDang(String userID, int page) {
		RequestParams params = new RequestParams();
		params.add("user_id", userID);
		params.add("page_number", String.valueOf(page));
		post(CTDADANG_URL+"?", params, asyncHttpResponseHandler);
		Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, CTDADANG_URL, params));
	}
	
	// cong thuc da thich
	public void getCongThucDaThich(String userID, int page) {
		RequestParams params = new RequestParams();
		params.add("user_id", userID);
		params.add("page_number", String.valueOf(page));
		post(CTDATHICH_URL+"?", params, asyncHttpResponseHandler);
		Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, CTDATHICH_URL, params));
	}
	
	// danh gia cong thuc
		public void ratingCongthuc(String user_id, String recipeId, int value){	
			RequestParams params = new RequestParams();
			params.add("user_id", user_id);
			params.add("recipe_id", recipeId);
			params.add("value", String.valueOf(value));
			post(RATING_CT+"?", params, asyncHttpResponseHandler);
			Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, RATING_CT, params));
		}
	
	/** USER */
	// lay thong tin user
	public void getInfoUser(String userId){
		RequestParams params = new RequestParams();
		params.add("user_id", userId);
		post(GET_INFO_USER+"?", params, asyncHttpResponseHandler);
		Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, GET_INFO_USER, params));
	}
	// lay activity user
	public void getInfoActivity(String userId, int page){
		RequestParams params = new RequestParams();
		params.add("user_id", userId);
		params.add("page_number", String.valueOf(page));
		post(USER_ACTIVITY+"?", params, asyncHttpResponseHandler);
		Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, USER_ACTIVITY, params));
	}
	
	// update url
	// lay activity user
	public void updateUrl(String userId, String userName){
		RequestParams params = new RequestParams();
		params.add("user_id", userId);
		params.add("username", userName);
		post(UPDATE_URL+"?", params, asyncHttpResponseHandler);
		Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, UPDATE_URL, params));
	}
	
	// lay activity user
	public void updateInfo(String userId, JSONObject data){
		RequestParams params = new RequestParams();
		params.add("user_id", userId);
		params.add("data", data.toString());
		post(UPDATE_INFO+"?", params, asyncHttpResponseHandler);
		Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, UPDATE_INFO, params));
	}
	
	
	// get thong bao
	public void getNofication(String userId, int page){
		RequestParams params = new RequestParams();
		params.add("user_id", userId);
		params.add("page_number", String.valueOf(page));
		post(GET_NOTIFI+"?", params, asyncHttpResponseHandler);
		Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, GET_NOTIFI, params));
	}
	
	
	
	/** BST */
	// get danh sach bo suu tap
	public void getBST(String userId){
		RequestParams params = new RequestParams();
		params.add("user_id", userId);
		post(GET_BST+"?", params, asyncHttpResponseHandler);
		Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, GET_BST, params));
	}
	
	// tao bo suu tap moi
	public void createNewBST(String userId, String name, String recipe_id){
		RequestParams params = new RequestParams();
		params.add("user_id", userId);
		params.add("name", name);
		params.add("recipe_id", recipe_id);
		post(CREATE_NEW_BST+"?", params, asyncHttpResponseHandler);
		Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, CREATE_NEW_BST, params));
	}
	
	// them mon an vao bst co san
	public void updateBST(String userId, String recipe_id, String collection_id){
		RequestParams params = new RequestParams();
		params.add("user_id", userId);
		params.add("recipe_id", recipe_id);
		params.add("collection_id", collection_id);
		post(UPDATE_BST+"?", params, asyncHttpResponseHandler);
		Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, UPDATE_BST, params));
	}
	
	// lay chi tiet cac mon an trong bo suu tap
	public void getDetailBST(String collection_id){
		RequestParams params = new RequestParams();
		params.add("collection_id", collection_id);
		post(DETAIL_BST+"?", params, asyncHttpResponseHandler);
		Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, DETAIL_BST, params));
	}
	
	// xoa cong thuc
	public void likeCongthuc(String userId, String recipeId){	
			RequestParams params = new RequestParams();
			params.add("user_id", userId);
			params.add("recipe_id", recipeId);
			post(LIKE_CT+"?", params, asyncHttpResponseHandler);
			Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, LIKE_CT, params));
	}
	
	
	/** TAO MOI CONG THUC */
	// gui len cho server de lay cai id
	public void getCreateCT(String user_id){
		RequestParams params = new RequestParams();
		params.add("user_id", user_id);
		post(CREATE_NEWCONGTHUC+"?", params, asyncHttpResponseHandler);
		Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, CREATE_NEWCONGTHUC, params));
	}
	
	// upload anh dai dien ct
	public void uploadImageCt(String user_id, String recipe_id,File myfile) throws FileNotFoundException{	
		RequestParams params = new RequestParams();
		params.add("user_id", user_id);
		params.add("recipe_id", recipe_id);
		params.add("browse_file", String.valueOf(myfile));
		post(UPLOAD_IMG_CT+"?", params, asyncHttpResponseHandler);
		Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, UPLOAD_IMG_CT, params));
	}
	
	// upload nguyen lieu
	public void uploadNguyenlieu(String user_id, String recipeId, JSONArray data){	
		RequestParams params = new RequestParams();
		params.add("user_id", user_id);
		params.add("recipe_id", recipeId);
		params.add("materials", data.toString());
		post(UPDATE_NGUYENLIEU+"?", params, asyncHttpResponseHandler);
		Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, UPDATE_NGUYENLIEU, params));
	}
	
	// update dacdiem
	public void updateDacdiem(String user_id, String recipeId, JSONArray data){	
		RequestParams params = new RequestParams();
		params.add("user_id", user_id);
		params.add("recipe_id", recipeId);
		params.add("params", data.toString());
		post(UPDATE_DACDIEM+"?", params, asyncHttpResponseHandler);
		Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, UPDATE_DACDIEM, params));
	}
	
	// upload nguyen lieu
	public void searchMonan(String keyword,JSONObject filter, int page_number, String type){	
		
		RequestParams params = new RequestParams();
		params.add("keyword", keyword);
		if (filter.toString().equals(null)) {
			params.add("filters", "{}");
		} else {
			params.add("filters", filter.toString());
		}
		params.add("filters", filter.toString());
		params.add("page_number", String.valueOf(page_number));
		params.add("sort", type);
		post(SEARCH_MONAN+"?", params, asyncHttpResponseHandler);
		Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, SEARCH_MONAN, params));
	}
	
	// xoa cong thuc
	public void deleteCongthuc(String userId, String recipeId){	
			RequestParams params = new RequestParams();
			params.add("user_id", userId);
			params.add("recipe_id", recipeId);
			post(DELETE_CT+"?", params, asyncHttpResponseHandler);
			Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, DELETE_CT, params));
	}
	
	
	// 	xoa cong thuc
	public void getThuoctinh(){	
		RequestParams params = new RequestParams();
		post(GET_ATTRIBUTE+"?", null, asyncHttpResponseHandler);
		Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, GET_ATTRIBUTE, null));
	}
	
	// save thu tu cac buoc cua cong thuc
	public void saveStepCT(String userID, String recipeID, JSONArray step){	
		RequestParams params = new RequestParams();
		params.put("user_id", userID);
		params.put("recipe_id", recipeID);
		params.put("step", step.toString());
		post(SAVE_STEP_CONGTHUC+"?", params, asyncHttpResponseHandler);
		Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, SAVE_STEP_CONGTHUC, params));
	}
	
	// save thu tu cac buoc cua cong thuc
	public void saveALLCT(String userID, String recipeID, String name, String desc){	
		RequestParams params = new RequestParams();
		params.put("user_id", userID);
		params.put("recipe_id", recipeID);
		params.put("name", name);
		params.put("desc", desc);
		post(SAVE_ALL_CONGTHUC+"?", params, asyncHttpResponseHandler);
		Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, SAVE_ALL_CONGTHUC, params));
	}
	
	
	// dang ki device id 
	public void updateDevice(String userID, String deviceType, String deviceID){	
		RequestParams params = new RequestParams();
		params.put("user_id", userID);
		params.put("device_type", deviceType);
		params.put("device_id", deviceID);
		post(UPDATE_DEVICE+"?", params, asyncHttpResponseHandler);
		Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, UPDATE_DEVICE, params));
	}
	
	// get all name cong thuc
	public void getAllNameCT(){	
		RequestParams params = new RequestParams();
		post(GET_ALL_CT, params, asyncHttpResponseHandler);
		Log.i("URL", "URL: "+getUrlPostWithParams(BASE_URL, GET_ALL_CT, params));
	}
	
}

